package cn.iocoder.yudao.module.reading.convert.community;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.comment.CommentCreateReqVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.comment.CommentRespVO;
import cn.iocoder.yudao.module.reading.dal.dataobject.community.CommunityCommentDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.reading.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.reading.framework.util.StaticResourceUrlUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 社区评论 Convert
 *
 * @author 芋道源码
 */
@Mapper
public interface CommunityCommentConvert {

    CommunityCommentConvert INSTANCE = Mappers.getMapper(CommunityCommentConvert.class);

    /**
     * 转换为评论DO
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "userId", ignore = true)
    @Mapping(target = "likeCount", constant = "0")
    @Mapping(target = "replyCount", constant = "0")
    @Mapping(target = "status", constant = "1")
    CommunityCommentDO convert(CommentCreateReqVO reqVO);

    /**
     * 转换为评论响应VO
     */
    default CommentRespVO convert(CommunityCommentDO commentDO, AdminUserDO userDO, Boolean isLiked) {
        if (commentDO == null) {
            return null;
        }

        CommentRespVO respVO = new CommentRespVO();
        respVO.setId(commentDO.getId());
        respVO.setPostId(commentDO.getPostId());
        respVO.setUserId(commentDO.getUserId());
        respVO.setParentId(commentDO.getParentId());
        respVO.setContent(commentDO.getContent());
        respVO.setLikeCount(commentDO.getLikeCount());
        respVO.setReplyCount(commentDO.getReplyCount());
        respVO.setCreateTime(commentDO.getCreateTime());
        respVO.setIsLiked(isLiked != null ? isLiked : false);

        // 设置用户信息
        if (userDO != null) {
            respVO.setUserNickname(userDO.getNickname());
            respVO.setUserAvatar(userDO.getAvatar());
        }

        return respVO;
    }

    /**
     * 转换为评论响应VO（支持reading模块用户）
     */
    default CommentRespVO convert(CommunityCommentDO commentDO, UserDO userDO, Boolean isLiked) {
        return convert(commentDO, userDO, isLiked, null);
    }

    /**
     * 转换为评论响应VO（支持reading模块用户，带URL转换）
     */
    default CommentRespVO convert(CommunityCommentDO commentDO, UserDO userDO, Boolean isLiked, StaticResourceUrlUtils urlUtils) {
        if (commentDO == null) {
            return null;
        }

        CommentRespVO respVO = new CommentRespVO();
        respVO.setId(commentDO.getId());
        respVO.setPostId(commentDO.getPostId());
        respVO.setUserId(commentDO.getUserId());
        respVO.setParentId(commentDO.getParentId());
        respVO.setContent(commentDO.getContent());
        respVO.setLikeCount(commentDO.getLikeCount());
        respVO.setReplyCount(commentDO.getReplyCount());
        respVO.setCreateTime(commentDO.getCreateTime());
        respVO.setIsLiked(isLiked != null ? isLiked : false);

        // 设置用户信息
        if (userDO != null) {
            respVO.setUserNickname(userDO.getNickname());
            // 处理用户头像URL转换
            if (urlUtils != null && userDO.getAvatar() != null) {
                respVO.setUserAvatar(urlUtils.toFullUrl(userDO.getAvatar()));
            } else {
                respVO.setUserAvatar(userDO.getAvatar());
            }
        }

        return respVO;
    }

    /**
     * 转换为评论响应VO列表
     */
    default List<CommentRespVO> convertList(List<CommunityCommentDO> commentList, List<AdminUserDO> userList, List<Boolean> likedList) {
        if (commentList == null) {
            return null;
        }

        return commentList.stream().map(comment -> {
            AdminUserDO user = userList.stream()
                    .filter(u -> u.getId().equals(comment.getUserId()))
                    .findFirst()
                    .orElse(null);

            Boolean isLiked = likedList != null && likedList.size() > commentList.indexOf(comment)
                    ? likedList.get(commentList.indexOf(comment))
                    : false;

            return convert(comment, user, isLiked);
        }).toList();
    }

    /**
     * 转换分页结果
     */
    default PageResult<CommentRespVO> convertPage(PageResult<CommunityCommentDO> pageResult, List<AdminUserDO> userList, List<Boolean> likedList) {
        if (pageResult == null) {
            return null;
        }

        PageResult<CommentRespVO> result = new PageResult<>();
        result.setTotal(pageResult.getTotal());
        result.setList(convertList(pageResult.getList(), userList, likedList));
        return result;
    }

}
